$.getScript("/media/js/jquery.dataTables.fnReloadAjax.js");

getMessages = function(url) {
	var result = null;
	$.ajax({
		url: url,
		type: 'get',
		dataType: 'json',
		async: false,
		success: function(data) {
			result = data;
		}
	});
	return result;
}

var form_popup_messages;
$(document).ready(function() {
	form_popup_messages = getMessages("/media/js/form_popup." + $("#lang").val() + ".txt");
});

$.fn.ShowOkPopup = function(message) {
	$("#ok-message-box").html(message);
	$("#ok-message-box").dialog('open');
	setTimeout(function(){$("#ok-message-box").dialog('close')}, 1500);
}

$.fn.LaunchPopup = function(){
	var args = arguments[0] || {};
	var WasAction = false;
	
	var TableId = args.TableId;
	var DivId = args.DivId;
	var FormId = args.FormId;
	var UrlForm = args.UrlForm;
	var Mode = args.Mode;
	var Title = args.Title;
	var ControlId = args.ControlId;
	var SaveButtonText = args.SaveButtonText;
	var DelButtonText = args.DelButtonText;
	var CloseButtonText = args.CloseButtonText;
	
	if (!SaveButtonText)
		SaveButtonText = form_popup_messages.save;
	if (!DelButtonText)
		DelButtonText = form_popup_messages.del;
	if (!CloseButtonText)
		CloseButtonText = form_popup_messages.close;
	
	$(DivId).dialog({
		position: ['center', 'center'],
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: [{
			id: 'dialog-button-save',
			text: SaveButtonText,
			style: 'font-weight: bold;',
			click: function() {
				WasAction = true;
				
				$("#dialog-button-save").attr('disabled', true);
				$("#dialog-button-save").find('span').html(form_popup_messages.wait);
				$.ajax({
					type: 'POST',
					url: UrlForm,
					data: $(FormId).serialize(),
					success: function(data) {
						$(FormId).html(data);
						$("#dialog-button-save").attr('disabled', false);
						$("#dialog-button-save").find('span').html(SaveButtonText);
						if ($(".ok").html().length != 0){
							$(DivId).dialog('close');
							$(this).ShowOkPopup($(".ok").html());
						}
					},
					error: function (error) {
						alert('error');
					}
				})
			}},
			{
			id: 'dialog-button-delete',
			text: DelButtonText,
			style: 'font-weight: bold;',
			click: function() {
				WasAction = true;
				
				$("#dialog-button-delete").hide();
				$.ajax({
					type: 'POST',
					url: UrlForm,
					data: $(FormId).serialize(),
					success: function(data) {
						$(FormId).html(data);
						if ($(".ok").html().length != 0){
							$(DivId).dialog('close');
							$(this).ShowOkPopup($(".ok").html());
						}
					},
					error: function (error) {
						alert('error');
					}
				})
			}},
			{
			id: 'dialog-button-close',
			text: CloseButtonText,
			click: function() {
				$('.ok').html('');
				$(DivId).find('input[type=text], textarea').val('');
				$(this).dialog("close");
			}
		}],
		open: function(event, ui) {
			$("span.ui-dialog-title").text(Title);
			if (($(FormId + " div").length > 0) && (!$(".info").html())) {
				if (Mode == 'add') {
					$('button[id*=dialog-button-save]').show();
					$('button[id*=dialog-button-delete]').hide();
				}
				if (Mode == 'edit') {
					$('button[id*=dialog-button-save]').show();
					$('button[id*=dialog-button-delete]').hide();
				}
				if (Mode == 'delete') {
					$('button[id*=dialog-button-save]').hide();
					$('button[id*=dialog-button-delete]').show();
				}
			}
			else {
				$('button[id*=dialog-button-save]').hide();
				$('button[id*=dialog-button-delete]').hide();
			}
		},
		close: function() {
			if (WasAction == true) {
				if (TableId) {
					oTableArr = TableId.split(",");
					for (i = 0; i <= oTableArr.length - 1; i++) {
						var oTable = $(oTableArr[i]).dataTable();
						oTable.fnReloadAjax();
					}
				}
				if (ControlId)
					$(this).SecondPopupClose();
			}
		}
	});
	
	$(FormId).load(UrlForm, function() {
		$(DivId).dialog('open');
		$(DivId + ' input, ' + DivId + ' select').first().trigger('focus');
	});
	
	return true;
};

$(document).keypress(function(e) { 
	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
		if ($('#dialog-form').dialog('isOpen') && (!$('#dialog-form').parent().find("button[id*=dialog-button-save]").attr('disabled')))
			$('#dialog-form').parent().find("button[id*=dialog-button-save]").click();
        return false;
	}
});
